package com.ruanyun.modular.ry.service.impl;

import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.ruanyun.base.pojo.page.LayuiPageFactory;
import com.ruanyun.base.consts.PublicCommon;
import com.ruanyun.base.pojo.page.LayuiPageInfo;
import com.ruanyun.modular.ry.api.vo.req.FeedBackReqVo;
import com.ruanyun.modular.ry.entity.Feedback;
import com.ruanyun.sys.core.shiro.ShiroKit;
import com.ruanyun.modular.ry.mapper.FeedbackMapper;
import com.ruanyun.modular.ry.model.params.FeedbackParam;
import com.ruanyun.modular.ry.model.result.FeedbackResult;
import  com.ruanyun.modular.ry.service.FeedbackService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 意见反馈 服务实现类
 * </p>
 *
 * @author conan
 * @since 2021-01-08
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    @Override
    public ResponseData add(FeedbackParam param){
        Feedback entity = getEntity(param);
        entity.setCreateTime(new Date());
        entity.setDelFlag(PublicCommon.DEL_FLAG_NORMAL);
        int result = this.baseMapper.insert(entity);
        if(result == 0){
            return ResponseData.error("添加失败");
        }
        return ResponseData.success();
    }

    @Override
    public void delete(FeedbackParam param){
        this.removeById(getKey(param));
    }

    /**
    * 删除
    *
    * @param id
    * @return
    */
    @Override
    public ResponseData updateDelFlagById(Long id) {
        Feedback entity = this.baseMapper.selectById(id);
        if (entity == null) {
            return ResponseData.error("该意见反馈不存在");
        }
        entity.setDelFlag(PublicCommon.DEL_FLAG_DELETE);
        int result = this.baseMapper.updateById(entity);
        if (result == 0) {
            return ResponseData.error("删除失败");
        }
        return ResponseData.success();
    }

    @Override
    public ResponseData update(FeedbackParam param){
        Feedback oldEntity = getOldEntity(param);
        Feedback newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        int result = this.baseMapper.updateById(newEntity);
        if(result == 0){
        return ResponseData.error("修改失败失败");
        }
        return ResponseData.success();
    }

    @Override
    public FeedbackResult findBySpec(FeedbackParam param){
        return null;
    }

    @Override
    public List<FeedbackResult> findListBySpec(FeedbackParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(FeedbackParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(FeedbackParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Feedback getOldEntity(FeedbackParam param) {
        return this.getById(getKey(param));
    }

    private Feedback getEntity(FeedbackParam param) {
        Feedback entity = new Feedback();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }
    /**
        * 启用
        *
        * @param id
        * @return
        */
    @Override
    public ResponseData enableById(Long id) {
        Feedback entity = this.baseMapper.selectById(id);
        if (entity == null) {
        return ResponseData.error("该意见反馈不存在");
        }
        entity.setDelFlag(PublicCommon.DEL_FLAG_NORMAL);
        int result = this.baseMapper.updateById(entity);
        if (result == 0) {
            return ResponseData.error("启用失败");
        }
        return ResponseData.success();
    }

    /**
     * 禁用
     *
     * @param id
     * @return
     */
    @Override
    public ResponseData disableById(Long id) {
        Feedback entity = this.baseMapper.selectById(id);
        if (entity == null) {
            return ResponseData.error("该意见反馈不存在");
        }
        entity.setDelFlag(PublicCommon.DEL_FLAG_DISABLE);
        int result = this.baseMapper.updateById(entity);
        if (result == 0) {
            return ResponseData.error("禁用失败");
        }
        return ResponseData.success();
    }

    /**
     * 添加意见反馈
     *
     * @param feedBackReqVo
     * @return
     */
    @Override
    public ResponseData insertFeedback(FeedBackReqVo feedBackReqVo, Long customerId) {
        Feedback feedback = new Feedback();
        BeanUtils.copyProperties(feedBackReqVo, feedback);
        feedback.setCreateTime(new Date());
        feedback.setCustomerId(customerId);
        feedback.setDelFlag(PublicCommon.DEL_FLAG_NORMAL);
        int insertCount = this.baseMapper.insert(feedback);
        if (insertCount == 0) {
            return ResponseData.error("反馈失败");
        }
        return ResponseData.success();
    }
}
